﻿namespace _20_原型模式;

public class CloneMode
{

    public void Show()
    {
        Person originalPerson = new Person("Alice", 30);

        Console.WriteLine($"Original Person: Name={originalPerson.Name}, Age={originalPerson.Age}");

        // 克隆Person对象
        Person clonedPerson = originalPerson.Clone();

        Console.WriteLine($"Cloned Person: Name={clonedPerson.Name}, Age={clonedPerson.Age}");

        // 修改克隆对象的属性，验证它们是独立的
        clonedPerson.Name = "Bob";
        clonedPerson.Age = 25;

        Console.WriteLine($"After modification:");
        Console.WriteLine($"Original Person: Name={originalPerson.Name}, Age={originalPerson.Age}");
        Console.WriteLine($"Cloned Person: Name={clonedPerson.Name}, Age={clonedPerson.Age}");
    }

    public interface IPrototype<T> where T : IPrototype<T>
    {
        T Clone();
    }

    public class Person : IPrototype<Person>
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public Person Clone()
        {
            return (Person)this.MemberwiseClone();
        }
    }
}